/*
 * WizardNode.java
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.teamskill.pux.library.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.CustomizableInputPorts;
import de.teamskill.pux.engine.CustomizableOutputPorts;
import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.OutputPort;

/**
 * WizardNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public abstract class WizardNode extends AbstractNode implements CustomizableInputPorts,
		CustomizableOutputPorts {

	/**
	 * IN_SHOW
	 */
	private static final String IN_SHOW = "SHOW";

	/**
	 * OUT_DATA
	 */
	private static final String OUT_DATA = "DATA";

	/**
	 * IN_DATA
	 */
	private static final String IN_DATA = "DATA";

	/**
	 * frame
	 */
	private Component content;

	/**
	 * show
	 */
	private InputPort show;

	/**
	 * outputData
	 */
	private OutputPort outputData;

	/**
	 * frame
	 */
	private JFrame frame;

	/**
	 * buttonPanel
	 */
	private JPanel buttonPanel;

	/**
	 * okButton
	 */
	private JButton okButton;

	/**
	 * cancelButton
	 */
	private JButton cancelButton;

	/**
	 * inputData
	 */
	private InputPort inputData;

	/**
	 * WizardNode Constructor
	 * 
	 */
	public WizardNode() {
		this("WIZARD" + nr++, false);
	}

	/**
	 * WizardNode Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public WizardNode(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * setupFrame
	 * 
	 */
	protected void setupFrame() {
		frame = GUISupport.getGUIFrame(getNetwork());
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setVisible(false);
		content = createContentPanel();
		frame.getContentPane().add(content, BorderLayout.CENTER);
		buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
		okButton = new JButton("OK");
		okButton.setMnemonic('O');
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actOK(e);
			}
		});
		buttonPanel.add(okButton);
		cancelButton = new JButton("Cancel");
		cancelButton.setMnemonic('C');
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actCancel(e);
			}
		});
		frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		frame.pack();
	}

	/**
	 * actCancel
	 * 
	 * @param e
	 */
	protected abstract void actCancel(ActionEvent e);

	/**
	 * actOK
	 * 
	 * @param e
	 */
	protected abstract void actOK(ActionEvent e);

	/**
	 * setupFrame
	 * 
	 */
	protected abstract Component createContentPanel();

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	@Override
	public void compute() {
		setupFrame();
		frame.setVisible(true);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	@Override
	protected void createPorts() {
		show = addInputPort(IN_SHOW, null, true);
		inputData = addInputPort(IN_DATA, null, false);
		outputData = addOutputPort(new OutputPort(OUT_DATA, this, null));
	}

	/**
	 * JavaBean-style getter-method for content
	 * 
	 * @return the content
	 */
	public Component getContent() {
		return content;
	}

	/**
	 * /** JavaBean-style setter-method for content
	 * 
	 * @param content
	 *            the content to set
	 */
	public void setContent(Component content) {
		this.content = content;
	}

	/**
	 * JavaBean-style getter-method for show
	 * 
	 * @return the show
	 */
	public InputPort getShow() {
		return show;
	}

	/**
	 * /** JavaBean-style setter-method for show
	 * 
	 * @param show
	 *            the show to set
	 */
	public void setShow(InputPort show) {
		this.show = show;
	}

	/**
	 * JavaBean-style getter-method for outputData
	 * 
	 * @return the outputData
	 */
	public OutputPort getOutputData() {
		return outputData;
	}

	/**
	 * /** JavaBean-style setter-method for outputData
	 * 
	 * @param outputData
	 *            the outputData to set
	 */
	public void setOutputData(OutputPort outputData) {
		this.outputData = outputData;
	}

	/**
	 * JavaBean-style getter-method for frame
	 * 
	 * @return the frame
	 */
	public JFrame getFrame() {
		return frame;
	}

	/**
	 * /** JavaBean-style setter-method for frame
	 * 
	 * @param frame
	 *            the frame to set
	 */
	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	/**
	 * JavaBean-style getter-method for buttonPanel
	 * 
	 * @return the buttonPanel
	 */
	public JPanel getButtonPanel() {
		return buttonPanel;
	}

	/**
	 * /** JavaBean-style setter-method for buttonPanel
	 * 
	 * @param buttonPanel
	 *            the buttonPanel to set
	 */
	public void setButtonPanel(JPanel buttonPanel) {
		this.buttonPanel = buttonPanel;
	}

	/**
	 * JavaBean-style getter-method for okButton
	 * 
	 * @return the okButton
	 */
	public JButton getOkButton() {
		return okButton;
	}

	/**
	 * /** JavaBean-style setter-method for okButton
	 * 
	 * @param okButton
	 *            the okButton to set
	 */
	public void setOkButton(JButton okButton) {
		this.okButton = okButton;
	}

	/**
	 * JavaBean-style getter-method for cancelButton
	 * 
	 * @return the cancelButton
	 */
	public JButton getCancelButton() {
		return cancelButton;
	}

	/**
	 * /** JavaBean-style setter-method for cancelButton
	 * 
	 * @param cancelButton
	 *            the cancelButton to set
	 */
	public void setCancelButton(JButton cancelButton) {
		this.cancelButton = cancelButton;
	}

	/**
	 * JavaBean-style getter-method for inputData
	 * 
	 * @return the inputData
	 */
	public InputPort getInputData() {
		return inputData;
	}

	/**
	 * /** JavaBean-style setter-method for inputData
	 * 
	 * @param inputData
	 *            the inputData to set
	 */
	public void setInputData(InputPort inputData) {
		this.inputData = inputData;
	}

	/**
	 * @see de.teamskill.pux.engine.CustomizableInputPorts#inputPortCreated(de.teamskill.pux.engine.InputPort)
	 */
	public void inputPortCreated(InputPort ip) {
	}

	/**
	 * @see de.teamskill.pux.engine.CustomizableInputPorts#inputPortModified(de.teamskill.pux.engine.InputPort)
	 */
	public void inputPortModified(InputPort ip) {
	}

	/**
	 * @see de.teamskill.pux.engine.CustomizableInputPorts#inputPortRemoved(de.teamskill.pux.engine.InputPort)
	 */
	public void inputPortRemoved(InputPort ip) {
	}

	/**
	 * @see de.teamskill.pux.engine.CustomizableOutputPorts#outputPortAdded(de.teamskill.pux.engine.OutputPort)
	 */
	public void outputPortAdded(OutputPort op) {
	}

	/**
	 * @see de.teamskill.pux.engine.CustomizableOutputPorts#outputPortModified(de.teamskill.pux.engine.OutputPort)
	 */
	public void outputPortModified(OutputPort op) {
	}

	/**
	 * @see de.teamskill.pux.engine.CustomizableOutputPorts#outputPortRemoved(de.teamskill.pux.engine.OutputPort)
	 */
	public void outputPortRemoved(OutputPort op) {
	}

}
